package com.neusoft.yuejian_6.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.neusoft.yuejian_6.R;
import com.neusoft.yuejian_6.javabean.User;
import com.neusoft.yuejian_6.model.UserModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RegActivity extends Activity {


    //声明
    private Button regbutton,loginbutton;
    private EditText username,userpass1,userpass2,phone,age;
    private String str_username;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏

        setContentView(R.layout.activity_reg);
        initViews();//初始化
        initEvents();//点击事件处理
        initData();
    }


    public void initViews(){
        username=(EditText) findViewById(R.id.username);
        userpass1=(EditText) findViewById(R.id.userpass1);
        userpass2=(EditText) findViewById(R.id.userpass2);
        phone=(EditText) findViewById(R.id.phone);
        age=(EditText) findViewById(R.id.age);

        loginbutton=(Button) findViewById(R.id.loginbutton);
        regbutton=(Button) findViewById(R.id.regbutton);
    }
    public void initEvents(){

        /*register_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 str_username=username.getText().toString();
                String str_userpass1 = userpass1.getText().toString();
                String str_userpass2 = userpass2.getText().toString();
                String str_tel = tel.getText().toString();
                String str_address = address.getText().toString();

                if (str_username.equals("")) {
                    Toast.makeText(RegisterActivity.this, "用户名为空", Toast.LENGTH_SHORT).show();
                    return;
                } else if (str_userpass1.equals("")) {
                    Toast.makeText(RegisterActivity.this, "密码为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                else if (str_userpass1.equals(str_userpass2) == false) {
                    Toast.makeText(RegisterActivity.this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }

                else {
                    UserModel userModel = new UserModel();
                    //1.发请求
                    Call<User> call = userModel.register
                            (str_username,str_userpass1,str_tel,str_address);

                    //2.处理结果
                    Callback<User> callback = new Callback<User>() {
                        public void onResponse(Call<User> call, Response<User> response) {

                            User user = response.body();
                            if (user.equals("0")) {
                                Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show();
                                Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
                                startActivity(intent);
                                finish();
                            }
                        }

                        @Override
                        public void onFailure(Call<User> call, Throwable t) {

                        }
                    };


                    //3.call and callback 捆绑
                    call.enqueue(callback);


                }
            }
        });*/
        loginbutton.setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent();
                        intent.setClass(RegActivity.this, LoginActivity.class);
                        startActivityForResult(intent, 2);

                    }
                });

        regbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                str_username=username.getText().toString();
                String str_userpass1 = userpass1.getText().toString();
                String str_userpass2 = userpass2.getText().toString();
                String str_phone = phone.getText().toString();

                String str_age = age.getText().toString();
                if (str_username.equals("")) {
                    Toast.makeText(RegActivity.this, "用户名为空", Toast.LENGTH_SHORT).show();
                    return;
                } else if (str_userpass1.equals("")) {
                    Toast.makeText(RegActivity.this, "密码为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                else if (str_userpass1.equals(str_userpass2) == false) {
                    Toast.makeText(RegActivity.this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }

                else {
                    UserModel userModel = new UserModel();
                    //1.发请求
                    Call<User> call = userModel.register
                            (str_username,str_userpass1,str_userpass2,str_phone,str_age);

                    //2.处理结果
                    Callback<User> callback = new Callback<User>() {
                        @Override
                        public void onResponse(Call<User> call, Response<User> response) {

                            String success = response.body().getSuccess();

                            if ("0".equals(success)) {
                                Toast toast = Toast.makeText(getApplicationContext(), "注册失败", Toast.LENGTH_SHORT);
                                toast.show();
                            } else {
                                String str_user = username.getText().toString();
                                Toast toast = Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT);
                                toast.show();
                                Intent intent = new Intent(RegActivity.this,LoginActivity.class);
                                intent.putExtra("username", str_user);
                                startActivityForResult(intent, 1);
                                finish();
                            }
                        }

                        @Override
                        public void onFailure(Call<User> call, Throwable t) {

                        }

                    };
                    //            Intent intent =new Intent();
                    //         intent.putExtra("username",str_username);
                    //       intent.setClass(RegActivity.this,LoginActivity.class);
                    //  finish();


                    //3.call and callback 捆绑
                    call.enqueue(callback);


                }
            }
        });
    }
    public void initData(){

    }


}


